# This is a sample Python script.

# Press ⌃R to execute it or replace it with your code.
# Press Double ⇧ to search everywhere for classes, files, tool windows, actions, and settings.
import time

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
import pandas as pd


def to_chinese(data_name):
    d = {
        "bond_id": "代码",
        "bond_nm": "转债名称",
        "price": "现价",
        "increase_rt": "涨跌幅",
        "stock_nm": "正股名称",
        "sprice": "正股价",
        "sincrease_rt": "正股涨跌",
        "pb": "正股PB",
        "convert_price": "转股价",
        "convert_value": "转股价值",
        "premium_rt": "溢价率",
        "bond_value": "纯债价值",
        "rating_cd": "债券评级",
        "_option_value": "期权价值",
        "volatility_rate": "正股波动率",
        "put_convert_price": "回售触发价",
        "force_redeem_price": "强赎触发价",
        "convert_amt_ratio": "转债占比",
        "fund_rt": "基金持仓",
        "short_maturity_dt": "到期时间",
        "year_left": "剩余年限",
        "curr_iss_amt": "剩余规模(亿元)",
        "volume": "成交额(万元)",
        "turnover_rt": "换手率",
        "ytm_rt": "到期税前收益",
        "_put_ytm_rt": "回售收益",
        "dblow": "双低",
        "cbOpt": "操作",
        "dblow_ranking": "双低排名"
    }
    return d.get(data_name) or data_name


def _get_attribute_value(attribute):
    value = attribute.text or attribute.find_element_by_tag_name("a").text
    return value


def get_convertible_bond_data_from_jisilu(save_to, top=None, user_name=None, password=None):
    chrome_options = Options()
    # chrome_options.add_argument('--headless')
    # chrome_options.add_argument('--disable-gpu')
    with webdriver.Chrome(chrome_options=chrome_options) as driver:
        driver.get("https://www.jisilu.cn/account/login/")
        driver.find_element_by_id("aw-login-user-name").send_keys(user_name)
        driver.find_element_by_id("aw-login-user-password").send_keys(password)
        driver.find_element_by_id("login_submit").click()
        cbnew = WebDriverWait(driver, 10).until(lambda d: d.find_element_by_link_text("可转债"))
        cbnew.click()
        driver.switch_to.window(driver.window_handles[-1])

        # table = driver.find_element(By.ID, "flex_cb")
        theads = WebDriverWait(driver, 10).until(lambda d: d.find_elements_by_xpath("//thead/tr[2]/th"))

        theads = [s.text.replace(" ", "").replace("\n", "") for s in theads]
        print(theads)
        removed_items = ["操作", "纯债价值", "期权价值", "正股波动率", "基金持仓", "回售收益"]
        removed_index = []
        for item in removed_items:
            removed_index.append(theads.index(item))
        removed_index = sorted(removed_index, reverse=True)
        for index in removed_index:
            theads.pop(index)
        print(removed_index)
        tbody = WebDriverWait(driver, 10).until(lambda d: d.find_element_by_tag_name("tbody"))
        bonds = tbody.find_elements(By.TAG_NAME, "tr")
        results = dict()
        if top:
            assert isinstance(top, int), f"top should be int not {type(top)}"
            bonds = bonds[:top]
        for bond in bonds:
            bond_id = bond.get_property("id")
            print(f"crawling {bond_id} data...")
            attributes = bond.find_elements(By.TAG_NAME, "td")
            # cbOpt:操作按钮,
            # 以下会员收费项目，无法获取
            # bond_value:纯债价值，_option_value：期权价值，volatility_rate：正股波动率, und_rt：基金持仓，_put_ytm_rt：回售收益
            # removed_item = ["cbOpt", "bond_value", "_option_value", "volatility_rate", "fund_rt", "_put_ytm_rt"]
            for index in removed_index:
                attributes.pop(index)
            s2 = time.time()

            # result = dict(list(map(_get_attribute_name_and_value, attributes)))
            result = dict(list(zip(theads, list(map(_get_attribute_value, attributes)))))

            print(time.time() - s2)
            results.update({bond_id: result})

        df = pd.DataFrame.from_records(list(results.values()))
        # print(df)
        # df = df.reset_index()    # reset index
        df = df.sort_values(by=to_chinese("dblow"), ascending=True)
        df[to_chinese("dblow_ranking")] = range(len(df))
        # theads.append("双低排名")
        # df = df[theads]
        order = ["代码", "转债名称", "现价", "溢价率", "双低", "双低排名", "剩余年限", "剩余规模(亿元)", "到期税前收益", "债券评级",
                 "涨跌幅", "正股名称", "正股价", "正股涨跌", "正股PB", "转股价", "转股价值", "回售触发价", "强赎触发价", "转债占比",
                 "到期时间", "成交额(万元)", "换手率"]
        df = df[order]
        print(df)
        df.to_excel(save_to, sheet_name="Convertible_bond", index=True)


# Press the green button in the gutter to run the script.
if __name__ == '__main__':
    start = time.time()
    name = ""
    passwd = ""
    get_convertible_bond_data_from_jisilu("~/Desktop/bond.xlsx", user_name=name, password=passwd)
    print(f"total: {time.time() - start}")

# See PyCharm help at https://www.jetbrains.com/help/pycharm/
